// 创建用户集合
// 引入mongoose模块
const mongoose = require('mongoose');

// 设置用户集合规则
const userSchema =  new mongoose.Schema({
  username: {
    type: String,
    required: true,
  },
  password: {
    type: String,
    required: true,
    minlength: 2,
    maxlength: 20,
  },
  email: {
    type: String,
    // 保存邮箱地址在插入数据库后不重复
    unique: true,
    required: true,
  },
  // admin 超级管理员
  // user 是普通用户
  role: {
    type: String,
    require: true,
  },
  // 0启用状态,
  // 1禁用状态
  state: {
    type: String,
    default: 0,
  }
});

// 创建集合
const Users = mongoose.model('Users', userSchema);

Users.create({
  username: 'itheima',
  password: '123456',
  role: 'admin',
  state: 0,
  email: '1119514898@qq.com',
}).then(() => console.log('用户创建成功'))
  .catch(err => console.log(err, '用户创建失败'));

module.exports = {
  Users,
}